import random

# 定义一个函数，用于模拟不放回抽样并计算概率
def calculate_same_color_probability(white_balls, red_balls, num_samples):
    total_balls = white_balls + red_balls
    same_color_counts = {'WW': 0, 'RR': 0}  # 字典来记录同色球的出现次数

    for _ in range(num_samples):
        # 从所有球中不放回地随机抽取两个球
        sampled_balls = random.sample(range(total_balls), 2)

        # 根据球的索引判断颜色（这里假设前white_balls个是白球，后red_balls个是红球）
        ball1_color = 'W' if sampled_balls[0] < white_balls else 'R'
        ball2_color = 'W' if sampled_balls[1] < white_balls else 'R'

        # 更新同色球的出现次数
        same_color_key = ball1_color + ball2_color
        same_color_counts[same_color_key] += 1

        # 计算并返回同色球的概率
    probability_WW = same_color_counts['WW'] / num_samples
    probability_RR = same_color_counts['RR'] / num_samples

    return probability_WW, probability_RR

# 调用函数并打印结果
white_balls = 4
red_balls = 2
num_samples = 10000  # 增加样本量以提高概率估计的准确性

probability_WW, probability_RR = calculate_same_color_probability(white_balls, red_balls, num_samples)

print(f"取到两个都是白球的概率为: {probability_WW:.4f}")
print(f"取到两个都是红球的概率为: {probability_RR:.4f}")